<?php
	include("initialize.php");
?>
<html>
<!--
The page from which a teacher accesses his class record.

@author Chad Estioco
@version Second Semester, AY 2010-2011
-->
<head>
	<title>GradeGrid - Class Operations</title>
	<?php include("header.php"); ?>
	<?php
		function passGETs($url){
			return $url . "?classid=" . $_GET['classid'];
		}
	?>
	<script type="text/javascript" language="javascript">
		function showExport(){
			document.getElementById("iframearea").style.display = "block"
		}
		
		function hideExport(){
			document.getElementById("iframearea").style.display = "none"
		}
		
		function getVars(){
			var classes_menu = document.getElementById("classes")
			var selected_class = classes_menu.options[classes_menu.selectedIndex].value;
			//selected_class = selected_class.split(" - ")
			var url = location.href.split("?")[0]
			location.href = url + "?classid=" + selected_class
		}
		
		function setSelected(course, section){
			var class_option = course + " - " + section
			var classes_menu = document.getElementById("classes")
			var i = 0
			var limit = classes_menu.options.length
			
			while(i < limit){
				if(classes_menu.options[i].text == class_option){
					classes_menu.selectedIndex = i
				}
				
				i++
			}
		}
		
		function saveGrades(){
			var gradespage = document.getElementById("gradeframe").contentDocument
			gradespage.getElementById("gradegrid").submit()
			alert("Grades saved.")
		}
	</script>
	<script type="text/javascript" src="../boilerplate/js/dragiframe.js"></script>
</head>
<body onLoad="writeCopyright()">
	<div class="main">
		<?php include("boilerplate.php"); ?>
		<?php include("loginnavigation.php"); ?>
		
		<?php if($_SESSION["logged_in"]): ?>
		<div style="float: left; min-width: 70%;">
			<p>View student record for class:
			<select id="classes" onchange="getVars()">
			</select></p>
			<?php
				include("write_functions.php");
				
				connect("localhost", "root", "", "GradeGridDatabase");
				
				//Get the details for this account.
				$account_query = sprintf("SELECT * FROM account WHERE username = '%s'",
				                         mysql_real_escape_string($_SESSION["username"]));
				$account = mysql_fetch_array(mysql_query($account_query));
				
				//Get all classes listed under this guy.
				$class_query = sprintf("SELECT class_id, class_name, course_name FROM class, course WHERE
				                        class.course_id = course.course_id and class.account_id = '%s'",
				                       mysql_real_escape_string($account["account_id"]));
				$cq = mysql_query($class_query);
				$class = mysql_fetch_array($cq);
				
				//$class_requested = $_GET['course'] . " - " . $_GET['section'];
				
				$i = 0;
				$limit = mysql_num_rows($cq);
				
				echo "<script type='text/javascript' language='javascript'>";
				
				while($i < $limit){
					mysql_data_seek($cq, $i);
					$class = mysql_fetch_array($cq);
					echo "var class = document.createElement('option')\n";
					$display = $class['course_name'] . " - " . $class['class_name'];
					echo "class.text = '" . $display . "'\n";
					echo "class.value = '" . $class["class_id"] . "'\n";
					echo "document.getElementById('classes').add(class, null)\n";
					
					if(!isset($_GET["classid"])){
						/*$_GET["course"] = $class["course_name"];
						$_GET["section"] = $class["class_name"];*/
						$_GET["classid"] = $class["class_id"];
					}
					
					if($_GET["classid"] == $class["class_id"]){
						echo "class.selected = true\n";
					}
					
					$i++;
				}
				
				echo "</script>";
				$grades = passGETs("grades.php");
				echo "<iframe style='width: 100%; min-height: 50%; border: none; border-width: 0px;' src='$grades' id='gradeframe'></iframe>";
			?>
			<br />
			<input type="button" value="Save Grades" onclick="saveGrades()" />
			<div id="iframearea" style="display: none; z-index: 1;">
				<?php
					$export_record = passGETs("export_record.php");
					echo "<iframe id='popup' class='popupbox' src='$export_record' frame='0'></iframe>";
				?>
				<!-- <iframe id="popup" name="popup" class="popupbox" src="export_record.php" frame="0">
				</iframe> -->
			</div>
		</div>
		<?php else: ?>
			<?php executeFile("../files/access_error.txt"); ?>
		<?php endif ?>
	</div>
	<div id="copywright"></div>
</body>
</html>